import {
  getFoodClass,
  findCanteenNameById
} from "../../apis/apis"
var app = getApp();
var apiUrl = getApp().globalData.apiUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canteenName: "第N饭堂",
    pickerIndex: 0,
    foodClassNameArr: [],
    foodClassArr: [],
    canteenId: 0,
    title: "",
    imageUrl: "",
    price: "",
    time: "",
    foodClass: 0,
    canteenArr: ["一饭一楼", "一饭三楼", "二饭", "三饭"],
    canteenPickerIndex: 0,
    disPicker: true,
    tags:""
  },

  pickerChange(e) {
    this.setData({
      pickerIndex: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    wx.setNavigationBarTitle({
      title: '悦食生活-添加菜品',
    })
    await this.setData({
      canteenId: parseInt(options.canteenId),
      canteenName: findCanteenNameById(options.canteenId, app.globalData.canteenList)
    })
    await this.onShow();
    if (app.globalData.userInfo.permission == 2) {
      this.setData({
        disPicker: false,
        canteenPickerIndex: parseInt(options.canteenId) - 1
      })
    } else {
      this.setData({
        canteenPickerIndex: app.globalData.userInfo.permission - 3
      })
    }
    
  },
  canteenPickerChange: function (e) {
    this.setData({
      canteenPickerIndex: e.detail.value,
      canteenName: this.data.canteenArr[this.data.pickerIndex],
      canteenId: parseInt(e.detail.value) + 1
    })
  },
  //添加菜品api
  postFood() {
    wx.request({
      url: app.globalData.apiUrl + '/food/addFood',
      method: "POST",
      data: {
        "token": wx.getStorageSync('token'),
        "title": this.data.title,
        "imageUrl": this.data.imageUrl,
        "price": parseInt(this.data.price),
        "food_class_id": parseInt(this.data.foodClassArr[this.data.pickerIndex].id),
        "canteen_id": parseInt(this.data.canteenId),
        "time": this.data.time,
        "tags": this.data.tags
      },
      success: (res) => {
        console.log(res);
        wx.showToast({
          title: '添加成功',
        })
        this.setData({
          title: "",
          imageUrl: "",
          price: "",
          time: 0,
          foodClass: 0
        })
      }
    })

  },
  titleChange: function (e) {
    this.setData({
      title: e.detail.value
    })
  },
  urlChange: function (e) {
    this.setData({
      imageUrl: e.detail.value
    })
  },
  priceChange: function (e) {
    this.setData({
      price: e.detail.value
    })
  },
  timeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },
  tagsChange:function(e){
    this.setData({
      tags:e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log(this.data.canteenId);
    getFoodClass(parseInt(this.data.canteenId)).then(res => {
      console.log(res);
      this.setData({
        foodClassArr: res
      })
      let arrayTemp = [""]
      for (let index = 0; index < this.data.foodClassArr.length; index++) {
        arrayTemp[index] = this.data.foodClassArr[index].foodClassName
      }
      this.setData({
        foodClassNameArr: arrayTemp
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})